import requests
import json
import time
import os


def get_data():
    collection_data = list()
    
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
    }

    for i in range(1, 65):
        print(f"{i}......")
        if i == 1:
            url = f"https://vimcolorschemes.com/page-data/index/page-data.json"
        else:
            url = f"https://vimcolorschemes.com/page-data/page/{i}/page-data.json"
        response = requests.get(url, headers = headers)

        content = response.content
        if content:
            data = json.loads(content, encoding = "utf-8")
            repositories = data["result"]["data"]["repositoriesData"]["apiRepositories"]
            
            for repository in repositories:
                name = repository["name"]
                github_url = repository["githubURL"]
                colorscheme = ""
                backgrounds = ""

                vimcolorschemes = repository["vimColorSchemes"]
                for vimcolorscheme in vimcolorschemes:
                    if vimcolorscheme["valid"] is True:
                        colorscheme = vimcolorscheme["name"]
                        backgrounds = ",".join(vimcolorscheme["backgrounds"])
                        break
                
                collection_data.append({
                    "name": name,
                    "github_url": github_url,
                    "color_scheme": colorscheme,
                    "backgrounds": backgrounds
                })

        #break
        print(f"{i}......OK")
        time.sleep(3)
    return collection_data


if __name__ == "__main__":
    #collection_data = get_data()
    
    base_dir = os.path.dirname(__file__)
    #with open(os.path.join(base_dir, "repositories.json"), "w", encoding = "utf-8") as f:
    #   json.dump(collection_data, f, ensure_ascii = False)

    with open(os.path.join(base_dir, "repositories.json"), "r", encoding = "utf-8") as f1:
        data = json.loads(f1.read(), encoding = "utf-8")
        for data_item in data:
            with open(os.path.join(base_dir, "repositories"), "a", encoding = "utf-8") as f2:
                f2.write(f'{data_item["name"]};{data_item["github_url"]};{data_item["color_scheme"]};{data_item["backgrounds"]}')
                f2.write("\n")

